﻿/**
@file main.cpp
@author onlytest
@brief main函数的定义
定义主函数的入口
*/
#include "SpriteGE.h"

using namespace Sprite;

class App:public Sprite::SpriteCore
{
public:
	void init()
	{
		SpriteCore::init();
		m_pCFps = new SpriteFps(this->m_pCMainTimer);
		m_pCFps->init();
		box = new Box();
		box->init(1.0f);
		box->BuildEffect(L"color.fx");
		box->BuildVertexLayouts();
		mTheta = 0.0f;
		mPhi = PI*0.25f;
	//	font = new SpriteFont();
	}

	void updateScene(float dt)
	{	
		// Code computes the average frames per second, and also the 
		// average time it takes to render one frame.
	
		SpriteCore::updateScene(dt);
		m_pCFps->update(dt);

		// Update angles based on input to orbit camera around box.
		if(GetAsyncKeyState('A') & 0x8000)	mTheta -= 2.0f*dt;
		if(GetAsyncKeyState('D') & 0x8000)	mTheta += 2.0f*dt;
		if(GetAsyncKeyState('W') & 0x8000)	mPhi -= 2.0f*dt;
		if(GetAsyncKeyState('S') & 0x8000)	mPhi += 2.0f*dt;
		
		// Restrict the angle mPhi.
		if( mPhi < 0.1f )	mPhi = 0.1f;
		if( mPhi > PI-0.1f)	mPhi = PI-0.1f;

		// Convert Spherical to Cartesian coordinates: mPhi measured from +y
		// and mTheta measured counterclockwise from -z.
		float x =  5.0f*sinf(mPhi)*sinf(mTheta);
		float z = -5.0f*sinf(mPhi)*cosf(mTheta);
		float y =  5.0f*cosf(mPhi);

		// Build the view matrix.
		D3DXVECTOR3 pos(x, y, z);
		D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
		D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
		m_pCMainCamera->init( &pos, &target, &up);
	}

	void drawScene()
	{
		SpriteCore::drawScene();
		assert(Sprite::g_pCD3d!=NULL);
		Sprite::g_pCD3d->begin();
		box->setLayout();
			box->m_WVP->SetMatrix(m_pCMainCamera->getViewMat()*Sprite::g_pCD3d->getProj());
		box->draw();
		
		m_pCFps->draw();
	//	font->draw(mFrameStats);
		Sprite::g_pCD3d->end();
	}

	int run()
	{
		return SpriteCore::run();
	}
	 void Test()
	 {
		 	MessageBox(0,L"fuck",L"fuck",0);
	 }
	 static App* CreateInstace(HINSTANCE hInstace)
	 {
		 Delete(m_pCCore);
		 m_pCCore = new App(hInstace);
		 return (App*)m_pCCore;
	 }
private:
	App(HINSTANCE hinstance):SpriteCore(hinstance)
	{
		
	}
	SpriteFps* m_pCFps;
	Box* box;

	float mTheta;
	float mPhi;
//	std::wstring mFrameStats;
//	SpriteFont * font;
};
/**
@brief WinMain函数的入口
定义了WinMain函数
*/
int WINAPI WinMain(  HINSTANCE hInstance,  HINSTANCE hPrevInstance,  LPSTR lpCmdLine,  int nShowCmd )
{
	App::CreateInstace(hInstance);
	App* win =dynamic_cast<App*>(App::Instance());
	win->init();
	return win->run();
}